<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "send_msg".
 *
 * @property int $id
 * @property string $title 标题
 * @property string $content 内容
 * @property int $type 类型
 * @property string $created_at 发送时间
 * @property int $user_id 发送人
 * @property int $status 状态
 * @property string $result 失败原因
 * @property string $mobile
 */
class SendMsg extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'send_msg';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['title', 'content'], 'required'],
            [['mobile'], 'required', 'message' => '请先选择接收人'],
            [['content'], 'string'],
            [['created_at', 'type', 'user_id', 'result', 'status'], 'safe'],
            [['title'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'mobile' => '接收人',
            'title' => '标题',
            'content' => '内容',
            'type' => '类型',
            'created_at' => '发送时间',
            'user_id' => '发送人',
            'status' => '状态',
            'result' => '失败原因',
        ];
    }

    /**
     * @inheritdoc
     * @return SendMsgQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new SendMsgQuery(get_called_class());
    }

    public function getSender()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public function getContact()
    {
        return $this->hasOne(CustomerContact::className(), ['mobile' => 'mobile']);
    }

    public function getCustomer()
    {
        return $this->hasOne(Customer::className(), ['id' => 'customer_id'])->via('contact');
    }
}
